package com.hyh.admin.sys.controller.base;


import com.hyh.ad.common.core.controller.BaseController;
import com.hyh.ad.common.core.domain.AjaxResult;
import com.hyh.ad.common.core.domain.model.SysRole;
import com.hyh.ad.common.core.page.TableDataInfo;
import com.hyh.ad.common.core.redis.CacheClient;
import com.hyh.admin.sys.service.ISysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RequestMapping("/api/sysRole")
@RestController
@Api(tags = "系统用户角色管理")
public class SysRoleController extends BaseController {

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private CacheClient cacheClient;

    /**
     * 查询所有角色
     * @param
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询所有角色")
    public TableDataInfo list(SysRole sysRole){
        startPage();
        List<SysRole> sysRoles = sysRoleService.selectListSysRolePage(sysRole);
        return getDataTable(sysRoles);
    }

    /**
     * 查询角色详情
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    @ApiOperation(value = "查询角色详情")
    public AjaxResult get(@PathVariable("id") Long id) {
        return AjaxResult.success(sysRoleService.selectSysRoleById(id));
    }

    /**
     * 新增角色
     * @param sysRole
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增角色")
    public AjaxResult add(@RequestBody SysRole sysRole) {
        return toAjax(sysRoleService.insertSysRole(sysRole));
    }

    /**
     * 修改角色
     * @param sysRole
     * @return
     */
    @PutMapping("/edit")
    @ApiOperation(value = "修改角色")
    public AjaxResult edit(@RequestBody SysRole sysRole) {
        return toAjax(sysRoleService.updateSysRole(sysRole));
    }

    /**
     * 删除角色
     * @param ids
     * @return
     */
    @DeleteMapping("/remove/{ids}")
    @ApiOperation(value = "删除角色")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysRoleService.deleteSysRoleByIds(ids));
    }

    /**
     * 为用户分配角色
     * @param userId
     * @param roleIds
     * @return 操作的结果
     */
    @PostMapping("/assignRoles")
    @ApiOperation(value = "为用户分配分配角色")
    public AjaxResult auth(@RequestParam("userId") Long userId, @RequestParam("roleIds") Long[] roleIds) {
        return toAjax(sysRoleService.assignRoles(userId, roleIds));
    }

    /**
     * 为角色分配权限
     * @param roleId
     * @param menuIds
     * @return 操作的结果
     */
    @PostMapping("/assignMenus")
    @ApiOperation(value = "为角色分配权限")
    public AjaxResult assignMenus(@RequestParam("roleId") Long roleId, @RequestParam("menuIds") Long[] menuIds) {
        return toAjax(sysRoleService.assignMenus(roleId, menuIds));
    }

    /**
     * 查询单个角色拥有的权限信息
     * @param  roleId
     * @return
     */
    @GetMapping("/getRoleMenus/{roleId}")
    @ApiOperation(value = "查询单个角色拥有的权限信息")
    public AjaxResult getRoleMenus(@PathVariable("roleId") Long roleId) {
        return AjaxResult.success(sysRoleService.selectRoleMenus(roleId));
    }

    /**
     * 更新角色状态
     * @param
     * @return
     */
    @PutMapping("/updateStatus")
    @ApiOperation(value = "更新角色状态")
    public AjaxResult updateStatus(@RequestBody SysRole sysRole) {
        return toAjax(sysRoleService.updateStatus(sysRole));
    }
}
